package com.jingdianjichi.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.jingdianjichi.subject.common.enums.isDeleteFlagEnum;
import com.jingdianjichi.subject.domain.convent.SubjectLabelConverter;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.domain.service.SubjectLabelDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectLabel;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMapping;
import com.jingdianjichi.subject.infra.basic.service.SubjectLabelService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 题目分类领域服务
 */
@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelService subjectLabelService;
    @Resource
    private SubjectMappingService subjectMappingService;

    @Override
    public void add(SubjectLabelBO subjectLabelBO) {
        // 日志参数校验
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.add.dto:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE
                .converBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(isDeleteFlagEnum.UN_DELETED.getCode());
        subjectLabelService.insert(subjectLabel);
    }


    /**
     * 查询标签 根据分类ID查询标签
     *
     * @param subjectLabelBO BO对象 用于查询条件
     * @return List<SubjectLabelBO> 一级分类集合数据
     */
    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        // 先拿到分类ID
        Long categoryId = subjectLabelBO.getCategoryId();
        // 根据分类ID查询标签
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(isDeleteFlagEnum.UN_DELETED.getCode());
        // 查询标签ID 根据分类ID和删除标识
        List<SubjectMapping> mappingList = subjectMappingService.queryLableId(subjectMapping);
        // 如果标签ID集合为空则返回空集合
        if (CollectionUtils.isEmpty(mappingList)) {
            return Collections.emptyList();
        }
        // 获取标签ID集合
        List<Long> lableList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        // 根据标签ID集合查询标签
        List<SubjectLabel> labelLists = subjectLabelService.batchQueryLabelById(lableList);
        // 创建BO集合用来存放查询到的标签
        LinkedList<SubjectLabelBO> boList = new LinkedList<>();
        labelLists.forEach(label -> {
            SubjectLabelBO bo = new SubjectLabelBO();
            bo.setId(label.getId());
            bo.setLabelName(label.getLabelName());
            bo.setCategoryId(categoryId);
            bo.setSortNum(label.getSortNum());
            boList.add(bo);
        });
        return boList;
    }

    /**
     * 更新分类
     *
     * @param subjectLabelBO 查询条件
     * @return 是否成功
     */
    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        // 日志参数校验
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.update.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        int count = subjectLabelService.update(SubjectLabelConverter.INSTANCE.converBoToLabel(subjectLabelBO));
        return count > 0;
    }

    /**
     * 删除分类
     *
     * @param subjectLabelBO 查询条件
     * @return 是否成功
     */
    @Override
    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        // 日志参数校验
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.delete.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.converBoToLabel(subjectLabelBO);
        // 设置删除标识
        subjectLabel.setIsDeleted(isDeleteFlagEnum.DELETED.getCode());
        return subjectLabelService.update(subjectLabel) > 0;
    }


}
